# -*- coding: utf-8 -*-
"""
@Time ： 2024/7/8 23:12
@Auth ： 九问
@File ：tasks.py
@IDE ：PyCharm
@Email : 2750826557@qq.com
"""
import logging

from django.core.mail import send_mail

from celery_tasks.celery_main import app
from BlackHoleDjango import settings

logger = logging.getLogger(__name__)



@app.task(name='send_email_task_tea')
def send_email_task_tea(email, username,password):
    """
    通知邮件任务
    :param email: 收件人邮箱
    :param username: 用户名
    :param password: 密码
    :return:
    """
    try:
        email_param = {
            'subject': 'BlackHoleDjango系统',
            'message': '',
            'html_message':
                f"""
                        <h2>欢迎加入BlackHoleDjango</h2>
                        <h3>诚邀您使用BlackHoleDjango</h3>
                        <p>您的登陆用户名为：<span style="color:#116bb7">{username}</span>(注：此用户名只能用于登录时)</p>
                        <p>您的登陆密码为：<span style="color:#116bb7">{password}</span></p>
                        <p><span style="color:#116bb7">登录之后请前往个人中心更新你的密码！！！！！</span></p>

                    """,
            'from_email': settings.EMAIL_HOST_USER,
            'recipient_list': [email]
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error('入职通知邮件发送[异常][ email: %s, message: %s]' % (email, e))
    else:
        if res_email:
            logger.info('入职通知邮件发送[正常][ email: %s, username: %s]' % (email, username))
        else:
            logger.warning('入职通知邮件发送[失败][ email: %s, username: %s]' % (email, username))


@app.task(name='send_email_task_stu')
def send_email_task_stu(email, username, classes_name):
    """
    通知邮件任务
    :param email: 收件人邮箱
    :param username: 用户名
    :param classes_name: 班级名
    :return:
    """
    try:
        email_param = {
            'subject': 'SecurityEye官方',
            'message': '',
            'html_message':
                f"""
                        <h2>欢迎加入BlackHoleDjango[{classes_name}]进行学习</h2>
                        <h3>诚邀您使用BlackHoleDjango</h3>
                        <p>其中包含：题目知识练习，技术学习讨论社区，相关书籍学习设备购物商场</p>
                        <p>您的登陆用户名为：<span style="color:#116bb7">{username}</span>(注：此用户名只能用于登录时)</p>
                        <p><span style="color:#116bb7">登录之后请前往个人中心更新你的密码！！！！！</span></p>

                    """,
            'from_email': settings.EMAIL_HOST_USER,
            'recipient_list': [email]
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error('入学通知邮件发送[异常][ email: %s, message: %s]' % (email, e))
    else:
        if res_email:
            logger.info('入学通知邮件发送[正常][ email: %s, username: %s]' % (email, username))
        else:
            logger.warning('入学通知邮件发送[失败][ email: %s, username: %s]' % (email, username))